# -*- mode: makefile; -*-

# Used to build stand-alone boot images that are loaded into a high address.
#
# Set these variables before inclusion:
#
# TOP		directory where this file resides
# IMAGE_ELF	name of final linked elf image (input)
# OBJS		list of all objects
#
include $(TOP)/make.cfg

LINK_SCRIPT=$(TOP)/boot/link-kernel.lds
START_ALONE=$(TOP)/boot/start-kernel.o

IMAGE_RAW=$(IMAGE_ELF).img

all:: .depend $(IMAGE_RAW)

# use mkimage to build a 'linux' kernel
$(IMAGE_RAW): $(IMAGE_ELF)
	@echo Building image $@
	@$(STRIP) $(IMAGE_ELF) -o $(IMAGE_ELF).strip
	@dd if=$(IMAGE_ELF).strip of=$(IMAGE_RAW).bin bs=32768 skip=1 > /dev/null 2>&1
	@rm $(IMAGE_ELF).strip
	@$(MKIMAGE) >/dev/null 2>&1 \
	-A arm -O linux -T kernel -C none \
	-a 0x80008000 -e 0x80008000 \
	-n $(IMAGE_ELF) -d $(IMAGE_RAW).bin $(IMAGE_RAW)
	@rm $(IMAGE_RAW).bin

# we need libgcc for eabi required functions such as for integer division
$(IMAGE_ELF): $(START_ALONE) $(OBJS) $(LIBOMAP)
	$(LD) -o $@ $^ -T$(LINK_SCRIPT) $(LIBGCC)

clean::
	-rm *.img $(IMAGE_ELF) *.bin *.strip
	-rm *.o

include $(TOP)/make.dep
